package com.sskj.gocoinapp.ui.activity;


import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.just.agentweb.AgentWeb;
import com.sskj.common.widget.DensityWebView;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.m.bean.Notice;
import com.sskj.gocoinapp.R;
import com.sskj.gocoinapp.bean.GuideListBean;
import com.sskj.gocoinapp.presenter.GuideWebActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;

import butterknife.BindView;

@Route(path = RConfig.APP_GUIDE_WEB)
public class GuideWebActivity extends BaseActivity<GuideWebActivityPresenter> {
    @BindView(R.id.webView)
    DensityWebView webView;
    @BindView(R.id.llWeb)
    LinearLayout llWeb;
    @Autowired
    String content;
    @Autowired
    String time;
    @Autowired
    String title;
    @Autowired
    String id;
    private AgentWeb mAgentWeb;
    @Autowired
    String url = "";
    @Autowired
    String head;


    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_guide_web;
    }

    @Override
    public GuideWebActivityPresenter getPresenter() {
        return new GuideWebActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(head);

        if (!TextUtils.isEmpty(url)) {
            if (!url.contains("http")) {
                url = "http://" + url;
            }
        }
        initWeb();
        if (TextUtils.isEmpty(url)) {
            updateUI();
        }
    }

    @Override
    protected void initData() {
        if (TextUtils.isEmpty(content)) {
            mPresenter.getContent(id);
        }
    }

    public void updateUI() {
        if (TextUtils.isEmpty(content)) {
            return;
        }
        String CSS_STYLE = "<style>* {font-size:40px;line-height:60px;} p {color:#fff;} a {color:#fff;} img {width:100%;} pre {font-size:40pt;line-height:60pt;font-family:Courier New,Arial;border:1px solid #ddd;border-left:5px solid #fff;background:#fff;padding:5px;}</style>";

        content = content.replaceAll("<span.*?</span><span.*?</span>", "").replace("“", "").replace("”", "");
        content = "<font style=\"color:#ffffff;font-size:60px;line-height:80px;\">" + title + "</font><br>\n" +
                "<div style=\"font-size:40px;text-align:left;margin-top:18px;color:#8E94A3\">" + time + "</div><br>"
                + "<hr size='0.1px',style=\"margin-top:18px;background-color:#7591b4\"></hr>"
                + "<div  style=\"font-size:40px;margin-top:40px;color:#1C1F22\">" + content + "</div" +
                ">";
        content = "<body style=\"background:#2D3033;margin:0;padding-left:15px;padding-right:15px;margin-top:20px\">" + content + "</body" +
                ">";
        mAgentWeb.getUrlLoader().loadDataWithBaseURL(HttpConfig.BASE_URL, CSS_STYLE + content, "text/html; charset=UTF-8", null, HttpConfig.BASE_URL);

    }

    public void initWeb() {
        ((ViewGroup) (webView.getParent())).removeView(webView);
        mAgentWeb = AgentWeb.with(this)//这里需要把 Activity 、 和 Fragment  同时传入
                .setAgentWebParent(llWeb, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(Color.TRANSPARENT)// 使用默认进度条
                .setWebView(webView)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK) //注意这里开启 strict 模式 ， 设备低于 4.2 情况下回把注入的 Js 全部清空掉 ， 这里推荐使用 onJsPrompt 通信
                .createAgentWeb()
                .ready()
                .go(url);
        WebSettings webSettings = mAgentWeb.getAgentWebSettings().getWebSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
//        webSettings.setTextZoom(200);
    }


    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
            Log.i("Info", "BaseWebActivity onPageStarted");
        }

        @Override
        public void onPageFinished(WebView webView, String url) {
        }
    };
    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);

        }
    };


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
        return true;
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }

    public void getContentSuccess(GuideListBean data) {
        content = data.getContent();
        updateUI();
    }
}
